package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.admin.pojo.Dish;
import com.itheima.service.DishService;
import com.itheima.util.RequestParesUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Author longbosheng
 * @Date 2022/6/15 15:14
 * @PackageName:com.itheima.controller
 * @ClassName: DishController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@RequestMapping("/dish")
@Log4j2
public class DishController {
    @Autowired
    public DishService dishService;
    /**
     * 添加菜品
     * @param dish
     * @return
     */
    @PostMapping
    public R addDish(@RequestBody Dish dish, HttpServletRequest request){
        /*取不到type值？*/
        String id = RequestParesUtil.RequestPares(request);
        dish.setType("1");
        dish.setCreateTime(new Date());
        dish.setCreateUser(id);
        dish.setUpdateTime(new Date());
        dish.setUpdateUser(id);
        System.out.println("控制层参数："+dish);
        return dishService.addDish(dish);
    }

    /**
     * 修改菜品
     * @param dish
     * @return
     */
    @PutMapping
    public R updateDish(@RequestBody Dish dish,HttpServletRequest request){
        String id = RequestParesUtil.RequestPares(request);
        dish.setUpdateUser(id);
        dish.setUpdateTime(new Date());
        return dishService.updateDish(dish);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteDish(@RequestParam List<String> ids){
        return dishService.deleteDish(ids);
    }

    /**
     * 批量菜品停售/起售
     * @param
     * @return
     */
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable int status,@RequestParam List<String> ids,HttpServletRequest request){
        String updateUser = RequestParesUtil.RequestPares(request);
        System.out.println("status:"+status);
        System.out.println("id列表："+ids);
        return dishService.updateDishStatus(status,ids,updateUser);
    }

    /**
     * 菜品下拉列表
     * @param
     * @return
     */
    @GetMapping("list")
    public R DropDownList(Dish dish){
        return dishService.DropDownList(dish);
    }

    /**
     * 菜品分页查询
     * @param name
     * @param page
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("/page")
    public R showDish(String name,int page,int pageSize,String type){
        System.out.println("类型的值为："+type);
        System.out.println("name的值为："+name);
        System.out.println("当前页为："+page);
        System.out.println("每页展示数为："+pageSize);
        /*收不到页面传来的type？*/
        if (type==null||type==""){
            type="1";
            int newtype = Integer.parseInt(type);
            if (name==null||name==""){
                return dishService.showDish(page,pageSize,newtype);
            }else {
                return dishService.showDishByName(name,page,pageSize,newtype);
            }
        }else {
            return R.error("查询控制层异常");
        }
    }

    /**
     * 按id查询菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectDishById(@PathVariable String id){
         return dishService.selectDishById(id);
    }
}
